<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Product\Category;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//商品分类
class ProductCategoryController extends Controller
{



    public function getCategorySelect(Request $request)
    {
        try {
            $Category = new Category();
            $data = $Category->getCategorySelect();
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $param = $request->all();
            $Category = new Category();
            $data = $Category->getAllCategory($param);
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
    public function add(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'name' => 'required|unique:zlg_category,name',
                ],
                [
                    'name.required' => '名称不能为空',
                    'name.unique' => '名称必须唯一',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Category();
            $Category->name = $data['name'];
            $res = $Category->save();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function show(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Category();
            $res = $Category->where("id", $data['id'])->first();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success($res);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    public function save(Request $request)
    {
        try {
            $data = $request->all();
            $id = $data['id'] ?? null;
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                    'name' => 'required|unique:zlg_category,name,' . $id,
                ],
                [
                    'id.required' => 'id不能为空',
                    'name.required' => '名称不能为空',
                    'name.unique' => '名称必须唯一',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Category();
            $res = $Category->where("id", $data['id'])->update(['name'=>$data['name']]);
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function del(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Category();
            $res = $Category->where("id", $data['id'])->delete();
            if (!$res) {
                throw new Exception('删除失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function getAllCategory() {}
}
